package com.platform.common.cache.redis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * 重写序列化 序列化为字节码
 */
@SuppressWarnings("rawtypes")
public class MyRedisSerializer implements RedisSerializer {

	@Override
	public byte[] serialize(Object o) throws SerializationException {
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ObjectOutputStream objOut;
		try {
			objOut = new ObjectOutputStream(byteOut);
			objOut.writeObject(o);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return byteOut.toByteArray();
	}

	@Override
	public Object deserialize(byte[] bytes) throws SerializationException {
		if (bytes == null)
			return null;
		ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
		ObjectInputStream objIn;
		Object obj;
		try {
			objIn = new ObjectInputStream(byteIn);
			obj = objIn.readObject();
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		}
		return obj;
	}
}
